package com.jihox.abookpro.dao;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jihox.abookpro.domain.*;
import com.jihox.abookpro.util.DBHelper;
import com.jihox.abookpro.util.MyMapperWrapper;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.mapper.MapperWrapper;

public class BookTemplateDAO {
	
	private String productsFile = null;
	private Context context;
	private DBHelper dbhelper;
	private SQLiteDatabase sqlitedatabase;
	
	public BookTemplateDAO(Context context) {
		super();
		this.context = context;
	}

	public BookTemplateDAO() {
		super();
		this.context = null;
	}
	
	public void opendb() {
		dbhelper = new DBHelper(context);
		sqlitedatabase = dbhelper.getWritableDatabase();
	}

	public void closedb() {
		if (sqlitedatabase != null && sqlitedatabase.isOpen()) {
			sqlitedatabase.close();
		}
	}
	
	//向数据库添加一条BookTemplate
	public void addBookTemplate(BookTemplate bookTemplate) {
		ContentValues cv = new ContentValues();
		cv.put("TemplateId", bookTemplate.getTid());
		cv.put("ProductId", bookTemplate.getProductId());
		cv.put("TemplateVersion", bookTemplate.getVersion());
		cv.put("Content", bookTemplate.getContent());
		opendb();
		sqlitedatabase.insert("templates", null, cv);
		closedb();
	}
	
	//从数据库删除一条BookTemplate
	public void deleteBookTemplate(String tid){
		opendb();
		try {
			sqlitedatabase.delete("templates", "TemplateId = ?", new String[] {tid});
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			closedb();
		}
	}
	
	//查询所有BookTemplate
	public List<BookTemplate> getBookTemplatesList() {
		ArrayList<BookTemplate> bookTemplatesList = new ArrayList<BookTemplate>();
		opendb();
		Cursor c = sqlitedatabase.rawQuery("SELECT * FROM templates", null);
		while (c.moveToNext()) {
			BookTemplate bookTemplate = new BookTemplate();
			bookTemplate.setTid(c.getString(c.getColumnIndex("TemplateId")));
			bookTemplate.setProductId(c.getString(c.getColumnIndex("ProductId")));
			bookTemplate.setVersion(c.getString(c.getColumnIndex("TemplateVersion")));
			bookTemplate.setContent(c.getString(c.getColumnIndex("Content")));
			bookTemplatesList.add(bookTemplate);
		}
		c.close();
		closedb();
		return bookTemplatesList;
	}
	
	//根据tid查询特定的BookTemplate
	public BookTemplate getBookTemplate(String tid) {
		BookTemplate bookTemplate = new BookTemplate();
		opendb();
		Cursor c = sqlitedatabase.rawQuery("SELECT * FROM templates where TemplateId = '" + tid +"'", null);
		while (c.moveToNext()) {
			bookTemplate.setTid(c.getString(c.getColumnIndex("TemplateId")));
			bookTemplate.setProductId(c.getString(c.getColumnIndex("ProductId")));
			bookTemplate.setVersion(c.getString(c.getColumnIndex("TemplateVersion")));
			bookTemplate.setContent(c.getString(c.getColumnIndex("Content")));
		}
		c.close();
		closedb();
		return bookTemplate;
	}
	
	//跟新BookProduct的内容
	public void updateBookTemplateContent(BookTemplate bookTemplate){
		ContentValues cv = new ContentValues();
		cv.put("Content", bookTemplate.getContent());
		opendb();
		sqlitedatabase.update("templates", cv, "TemplateId = ? ",
				new String[] { bookTemplate.getTid()});
		closedb();
	}
	
	//跟新BookProduct的version
	public void updateBookTemplateVersion(BookTemplate bookTemplate){
		ContentValues cv = new ContentValues();
		cv.put("TemplateVersion", bookTemplate.getVersion());
		opendb();
		sqlitedatabase.update("templates", cv, "TemplateId = ? ",
				new String[] { bookTemplate.getTid()});
		closedb();
	}
	
	
	public BookTemplate retrieveBookTemplate(InputStream is)
			throws Exception {
		BookTemplate template = null;
		if (is == null) {
			return template;
		}

		try {
			XStream xStream = new XStream(new DomDriver()) {
				@Override
				protected MapperWrapper wrapMapper(MapperWrapper next) {
					return new MyMapperWrapper(next);
				}
			};
			xStream.alias("BookTemplate", BookTemplate.class);
			xStream.alias("Page", Page.class);
			xStream.alias("PagePicture", PagePicture.class);
			xStream.alias("Caption", Caption.class);
			xStream.alias("Color", Color.class);
			xStream.addImplicitCollection(BookTemplate.class, "pages",Page.class);
			xStream.addImplicitCollection(Page.class, "pictures",PagePicture.class);
			xStream.addImplicitCollection(Page.class, "captions",Caption.class);
			template = new BookTemplate();
			template = (BookTemplate) xStream.fromXML(is, template);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return template;
	}
	
	//从数据库中拿到Content恢复成BookTemplate
	public BookTemplate retrieveBookTemplate(String str){
		BookTemplate template = null;
		if (str == null) {
			return template;
		}
		try {
			XStream xStream = new XStream(new DomDriver()) {
				@Override
				protected MapperWrapper wrapMapper(MapperWrapper next) {
					return new MyMapperWrapper(next);
				}
			};
			xStream.alias("BookTemplate", BookTemplate.class);
			xStream.alias("Page", Page.class);
			xStream.alias("PagePicture", PagePicture.class);
			xStream.alias("Caption", Caption.class);
			xStream.alias("Color", Color.class);
			xStream.addImplicitCollection(BookTemplate.class, "pages",Page.class);
			xStream.addImplicitCollection(Page.class, "pictures",PagePicture.class);
			xStream.addImplicitCollection(Page.class, "captions",Caption.class);
			template = new BookTemplate();
			template = (BookTemplate) xStream.fromXML(str, template);
		} catch (Exception ex) {
			ex.printStackTrace();
		} 
		return template;
	}

}
